﻿using AvalonDock;
using RiskMan.Config;
using RiskMan.DataClass.Models.Gilevskiy;
using RiskMan.DataClass.Models.NotBasModels;
using RiskMan.Views.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace RiskMan.Views.Gilevskiy
{
    /// <summary>
    /// Interaction logic for GilevskiyWindow.xaml
    /// </summary>
    public partial class GilevskiyWindow : DocumentContent
    {
        private readonly MainWindow mainWindow;
        private readonly DispatcherTimer _dsTimer = new DispatcherTimer();
        public GilevskiyWindow()
        {
            mainWindow = MainWindow.StaticLevel;
            InitializeComponent();

            DtgdRobotGilevskiy.ItemsSource = mainWindow._dataProvider.RobotGilevskiyItems;
        }

        private void Button_AddRow_Click(object sender, RoutedEventArgs e)
        {
            RobotGilevskiyItem item = new RobotGilevskiyItem();
            AddNewRobot window = new AddNewRobot(item, mainWindow._dataProvider);
            window.ShowDialog();

            if (window.DialogResult == true)
                mainWindow._dataProvider.RobotGilevskiyItems.Add(item);

            //Update();
            Cfg.SetLog("Добавлены новые условия", mainWindow.ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (DtgdRobotGilevskiy.SelectedItems.Count == 0)
                new DialogMessage("Не выбрано ни одного элемента для удаления", "Внимание!");

            if (DtgdRobotGilevskiy.SelectedItems.Count == 1)
            {
                int index = DtgdRobotGilevskiy.SelectedIndex + 1;
                try
                {
                    if (new DialogOkCancel("Вы действительно хотите удалить строку " + index + "?", "Внимание").Result == MessageBoxResult.OK)
                    {
                        foreach (var item in mainWindow._dataProvider.RobotGilevskiyItems)
                        {
                            if (item.GuidItem == ((RobotGilevskiyItem)(DtgdRobotGilevskiy.SelectedItem)).GuidItem)
                            {
                                mainWindow._dataProvider.RobotGilevskiyItems.Remove(item);
                                break;
                            }
                        }

                    }
                }
                catch
                { }
            }

        }

        private void BtnCopy_Click(object sender, RoutedEventArgs e)
        {
            if (DtgdRobotGilevskiy.SelectedItems.Count == 1)
            {
                int index = DtgdRobotGilevskiy.SelectedIndex + 1;
                try
                {
                    if (new DialogOkCancel("Вы действительно хотите скопировать строку " + index + "?", "Внимание").Result == MessageBoxResult.OK)
                    {
                        foreach (var item in mainWindow._dataProvider.RobotGilevskiyItems)
                        {
                            if (item.GuidItem == ((RobotGilevskiyItem)(DtgdRobotGilevskiy.SelectedItem)).GuidItem)
                            {
                                var tmp = (RobotGilevskiyItem)Cfg.DeepClone(item);
                                tmp.IsRun = false;
                                tmp.GuidItem = Guid.NewGuid();
                                tmp.TradeList = new System.Collections.ObjectModel.ObservableCollection<TradesOpen>();
                                mainWindow._dataProvider.RobotGilevskiyItems.Add(tmp);
                            }
                        }

                    }
                }
                catch
                { }
            }
        }

        private void GilevskiyWindowLayout_Loaded(object sender, RoutedEventArgs e)
        {
            _dsTimer.Interval = TimeSpan.FromMilliseconds(1000);
            _dsTimer.Start();
            _dsTimer.Tick += new EventHandler(_dsTimer_Tick);
        }

        public void _dsTimer_Tick(Object sender, EventArgs args)
        {
            //if (mainWindow.ConnectingOrDisconnecting())
            //{
            if (mainWindow._dataProvider.RobotGilevskiyItems.Count > 0)
            {
                foreach (var item in mainWindow._dataProvider.RobotGilevskiyItems)
                {
                    if (item.IsRun == true)
                    {
                        LogikRobot logik = new LogikRobot(mainWindow);
                        logik.Start(item);
                    }
                }

            }
            //}
        }

        private void DtgdRobotGilevskiy_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            for (int i = 0; i < mainWindow._dataProvider.RobotGilevskiyItems.Count; i++)
            {
                if (((DataGrid)sender).SelectedIndex == i)
                    DtgdOrders.ItemsSource = mainWindow._dataProvider.RobotGilevskiyItems[i].TradeList;
            }
        }

        private void DtgdRobotGilevskiy_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (DtgdRobotGilevskiy.SelectedIndex >= 0)
            {
                RobotGilevskiyItem ldItem = ((DataGrid)sender).SelectedItem as RobotGilevskiyItem;
                AddNewRobot addNewRobot = new AddNewRobot(ldItem, mainWindow._dataProvider);
                addNewRobot.ShowDialog();
            }
        }

    }
}
